package com.qk.management.controller;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Enid
 * @date: 2025/10/7 18:14
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        User user = userService.selectById(id);
        return Result.success(user);
    }

    /**
     * 批量删除用户，使用集合接收id
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable("ids") List<Integer> ids){
        userService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/role/{roleLabel}")
    public Result listOfRole(@PathVariable("roleLabel") String roleLabel){
        List<User> list = userService.listOfRole(roleLabel);
        return Result.success(list);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        userService.addUser(user);
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO userDTO){
        PageResult<UserDO> pageResult = userService.list(userDTO);
        return Result.success(pageResult);
    }




    @GetMapping("/list")
    public Result list(){
        List<User> all = userService.all();
        return Result.success(all);
    }


    @GetMapping("/dept/{deptId}")
    public Result listOfDept(@PathVariable("deptId") Integer deptId){
        List<User> list = userService.listOfDept(deptId);
        return Result.success(list);
    }
}
